package com.mall.web.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 方案
 *
 */
@Service
public class ProgrammeService {
	Logger logger = LoggerFactory.getLogger(ProgrammeService.class);
	@Resource JdbcTemplate jdbcTemplate;
	public Map<String, Object> find_user_programme(int direction, int fraction,String min_age, String max_age, String user_id, int type2) {
		String sql="";
		try {
			if(StringUtils.isEmpty(max_age)){
				sql="select * from t_e_programme where min_age=? and direction =? and fraction>=? and user_id=?  order by create_time desc limit 0,1";
				Map<String, Object> queryForMap = jdbcTemplate.queryForMap(sql,min_age,direction,fraction,user_id);
				return queryForMap;
			}else{
				sql="select * from t_e_programme where min_age>=? and max_age<=? and direction =? and fraction=? and user_id=? order by create_time desc  limit 0,1";
				Map<String, Object> queryForMap = jdbcTemplate.queryForMap(sql,min_age,max_age,direction,fraction,user_id);
				return queryForMap;
			}
		} catch (Exception e) {
			return null;
		}
	}
	public List<Map<String, Object>> find_user_all_news(int direction,int fraction, String min_age, String max_age, String user_id, int type2) {
		String sql="";
		List<Map<String, Object>> queryForList=null;
		
		if(StringUtils.isEmpty(max_age)){
			sql="select title,link,id,direction from t_e_individualization where min_age=? and direction =? and fraction>=? and identity=?  order by create_time desc ";
//			sql="select title,link,id,direction from t_e_individualization where min_age=? and direction =? and fraction>=? and identity=? group by direction  order by create_time desc ";
			queryForList = jdbcTemplate.queryForList(sql,min_age,direction,fraction,type2);
		}else{
			sql="select title,link,id,direction from t_e_individualization where min_age>=? and max_age<=? and direction =? and fraction>=?  and identity=?  order by create_time desc  ";
//			sql="select title,link,id,direction from t_e_individualization where min_age>=? and max_age<=? and direction =? and fraction>=? and identity=?  group by direction  order by create_time desc  ";
			queryForList = jdbcTemplate.queryForList(sql,min_age,max_age,direction,fraction,type2);
		}
		
		Map<String, Object> find_user_programme = find_user_programme(direction, fraction, min_age, max_age, user_id,type2);
		int type=0;
		if(null!=find_user_programme && find_user_programme.size()>0){
			 Integer integer = MapUtils.getInteger(find_user_programme, "type");
			 type=integer+1;
		}else{
			type=1;
		}
		if(null!=queryForList){
			for (Map<String, Object> map : queryForList){
				int new_id = Integer.parseInt(String.valueOf(map.get("id")));
				sql="select count(*) from t_e_programme where new_id=?";
				int queryForInt = jdbcTemplate.queryForInt(sql,new_id);
				if(queryForInt==0){
					sql="insert into t_e_programme (min_age,max_age,direction,fraction,new_id,user_id,create_time,type) values(?,?,?,?,?,?,date_format(now(),'%Y-%m-%d'),?)";
					jdbcTemplate.update(sql,min_age,max_age,direction,fraction,new_id,user_id,type);
				}
			}
		}
		
		return queryForList;
	}
	public List<Map<String, Object>> find_user_all_news_history(int direction,int fraction, String min_age, String max_age, String user_id, int type) {
		String sql="";
		List<Map<String, Object>> queryForList=null;
		if(StringUtils.isEmpty(max_age)){
			sql="select * from t_e_programme where min_age=? and direction =? and fraction>=? and user_id=?  order by create_time desc";
			queryForList = jdbcTemplate.queryForList(sql,min_age,direction,fraction,user_id);
		}else{
			sql="select * from t_e_programme where min_age>=? and max_age<=? and direction =? and fraction=? and user_id=? order by create_time desc  ";
			queryForList = jdbcTemplate.queryForList(sql,min_age,max_age,direction,fraction,user_id);
		}
		String ids="";
		for (Map<String, Object> map : queryForList){
			String new_id = String.valueOf(map.get("new_id"));
			if(StringUtils.isEmpty(ids)){
				ids=new_id;
			}else{
				ids=ids+","+new_id;
			}
		}
		sql="select title,link,id,direction from t_e_individualization where id in("+ids+") group by id ";
		queryForList = jdbcTemplate.queryForList(sql);
		return queryForList;
	}
	public int save_user_history(Map<String, Object> paraMap) {
		String open_id = MapUtils.getString(paraMap, "open_id");
		String new_id = MapUtils.getString(paraMap, "new_id");
		int type = MapUtils.getIntValue(paraMap, "type");
		
		String sql="select count(*) from t_e_user_history where new_id=? and open_id=?";
		int queryForInt = jdbcTemplate.queryForInt(sql,new_id,open_id);
		if(queryForInt==0){
			if(type==0){
				sql="insert into t_e_user_history(new_id,open_id,type,create_time) values(?,?,1,now())";
			}else{
				sql="insert into t_e_user_history(new_id,open_id,type,create_time) values(?,?,2,now())";
			}
			int update = jdbcTemplate.update(sql,new_id,open_id);
			return update;
		}
		return 0;
	}
}
